#include "snake_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 创建头节点
snake_data_head_p snake_data_head_create()
{
    snake_data_head_p head = (snake_data_head_p)malloc(sizeof(snake_data_head_t));
    if (head == NULL)
    {
        printf("malloc failed\n");
        return NULL;
    }
    head->length = 0;
    head->head = NULL;
    return head;
}

// 创建节点
snake_data_p snake_data_create(char * name , int score)
{
    snake_data_p node = (snake_data_p)malloc(sizeof(snake_data_t));
    if (node == NULL) {
        printf("malloc failed\n");
        return NULL;
    }
    // 使用strncpy并确保字符串以'\0'结尾
    strncpy(node->name, name, sizeof(node->name) - 1);
    node->name[sizeof(node->name) - 1] = '\0';
    node->score = score;
    node->prev = NULL;
    node->next = NULL;
    return node;
}


// 增加节点
void snake_data_add(snake_data_head_p head, snake_data_p node)
{
    if (head->length == 0)
    {
        head->head = node;
    }
    else
    {
        node->next = head->head;
        head->head->prev = node;
        head->head = node;
    }
    head->length++;
}

// 删除节点
void snake_data_remove(snake_data_head_p head, snake_data_p node)
{
    if (node == NULL || head == NULL) return;
    if (node == head->head)
    {
        head->head = node->next;
    }
    if (node->next != NULL)
    {
        node->next->prev = node->prev;
    }
    if (node->prev != NULL)
    {
        node->prev->next = node->next;
    }
    head->length--;
    free(node);
}

// 排序
void snake_data_sort(snake_data_head_p head)
{
    snake_data_p node = head->head;
    while (node != NULL)
    {
        snake_data_p next = node->next;
        while (next != NULL)
        {
            if (node->score < next->score)
            {
                int temp_score = node->score;
                char temp_name[30];
                strcpy(temp_name, node->name);
                node->score = next->score;
                strcpy(node->name, next->name);
                next->score = temp_score;
                strcpy(next->name, temp_name);
            }
            next = next->next;
        }
        node = node->next;
    }
}

// 打印链表
void snake_data_print(snake_data_head_p head)
{
    snake_data_p node = head->head;
    while (node != NULL)
    {
        printf("%s:%d\n", node->name, node->score);
        node = node->next;
    }
}

// 销毁链表
void snake_data_destroy(snake_data_head_p head)
{
    snake_data_p node = head->head;
    while (node != NULL)
    {
        snake_data_p next = node->next;
        free(node);
        node = next;
    }
    free(head);
}

// 按行打开文件
snake_data_head_p snake_data_open(char *filename)
{
    FILE *fp = fopen(filename, "r"); // 改为文本模式 "r"
    if (fp == NULL) {
        printf("open file failed\n");
        return NULL;
    }
    else {
        printf("open file success\n");
    }

    snake_data_head_p head = snake_data_head_create();
    if (head == NULL) {
        printf("malloc failed\n");
        fclose(fp);
        return NULL;
    }

    char line[100]; // 缓冲区，确保足够大以容纳一行内容
    char name[30];
    int score;

    while (fgets(line, sizeof(line), fp) != NULL) { // 安全地读取一行
        // 使用 sscanf 从行缓冲区中解析数据，格式字符串调整同上
        int n = sscanf(line, "%29[^,],%d", name, &score);
        printf("n:%d\n", n);
        if (n == 2) {
            printf("fopen:%s,%d\n", name, score);
            snake_data_p node = snake_data_create(name, score);
            if (node == NULL) {
                fclose(fp);
                snake_data_destroy(head);
                return NULL;
            }
            snake_data_add(head, node);
        } else {
            // 可以在这里打印警告，忽略格式不匹配的行
            printf("Warning: Skipping malformed line: %s", line);
        }
    }

    fclose(fp);
    return head;
}

// 按行保存文件
void snake_data_save(snake_data_head_p head, char *filename)
{
    FILE *fp = fopen(filename, "wb");
    if (fp == NULL)
    {
        printf("open file failed\n");
        return;
    }
    snake_data_p node = head->head;
    while (node != NULL)
    {
        fprintf(fp, "%s,%d\n", node->name, node->score);
        node = node->next;
    }
    fclose(fp);
}

// 向文件中直接添加一行数据
void snake_data_add_line(const char *name, int score){
    FILE *fp = fopen("snake_data.txt", "a+"); // 改为追加模式 "a+"
    if (fp == NULL) {
        printf("open file failed\n");
        return;
    }
    fprintf(fp, "%s,%d\n", name, score);
    fclose(fp);
}

